<template>
  <div class="addressEdit">
    <van-address-edit :area-list="areaList" show-set-default show-search-result :show-delete="isEdit"  
      :search-result="searchResult" :area-columns-placeholder="['请选择', '请选择', '请选择']" @save="onSave" @delete="onDelete"
      :address-info="{
          name: addressInfo.name,
          tel: addressInfo.tel,
          addressDetail:addressDetail,
          idDefault:addressInfo.isDefault
        }"
    />
  </div>
</template>

<script>
  import {addAddress,delAddress} from '@/request/api.js'
import { areaList } from '@vant/area-data';
var _ = require("lodash");
export default {
  data() {
    return {
      isEdit:false,
      areaList,
      searchResult: [],
      addressInfo:{},
      addressDetail:'',
      addressId:-1,
    };
  },
  mounted(){
    if(this.$route.query.isEdit) this.isEdit = JSON.parse(this.$route.query.isEdit)
    if(this.$route.query.address){
      this.addressInfo = JSON.parse(this.$route.query.address)
      this.addressDetail = this.addressInfo.address.substr(9-this.addressInfo.address.length)
      this.addressId = this.addressInfo.id
    }
    
  },
  methods: {
    onSave(val) {
      val.userId= this.$store.state.userId
      if(this.isEdit){

      }else{
        addAddress(val).then(res=>{
          this.$toast.success(res.message)
          this.$router.back()
        })
      }
    },
    onDelete() {
      if(this.addressId!=-1){
        delAddress({id:this.addressId}).then(res=>{
          this.$toast.success(res.message)
          this.$router.back()
        })
      }
    },
  },
};
</script>

<style scoped>
.addressEdit{
  margin-top: 50px;
}
</style>